//
//  SVProgressHUD+CustomStyle.m
//  SVProgressHUD
//
//  Created by 蔡儒楠 on 2017/4/19.
//  Copyright © 2017年 DingliTech. All rights reserved.
//

#import "SVProgressHUD+CustomStyle.h"

static NSTimeInterval const minDalayTime = 2.f;
static NSInteger const maxMsgLength = 20;

@implementation SVProgressHUD (CustomStyle)

#pragma mark - /** 需要手动关闭 */
+ (void)showMessage:(NSString *)message {
    [self showSVProgressHUD:message UserInteraction:NO];
}

+ (void)showMesaggeEnabledUserInteraction:(NSString *)message {
    [self showSVProgressHUD:message UserInteraction:YES];
}

+ (void)showSVProgressHUD:(NSString *)message UserInteraction:(BOOL)enabled {
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    
    if (enabled) {
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
    } else {
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    }
    
    NSString *showMessage = message;
    if (showMessage.length > maxMsgLength) {
        [self showMessageSustain:showMessage mask:SV_Result_Warning completion:nil];
    } else if (showMessage.length>0 && showMessage.length<=maxMsgLength) {
        [self showWithStatus:showMessage];
    } else {
        [self show];
    }
}

#pragma mark - /** 最长2秒后自动消失 */
+ (void)showMessageSustain:(NSString *)message mask:(SVProgressHudMaskResultIcon)maskType {
    [self showMessageSustain:message mask:maskType completion:nil];
}

+ (void)showMessageSustain:(NSString *)message
                      mask:(SVProgressHudMaskResultIcon)maskType
                completion:(SV_ShowHUDCompletion)completedBlock {
    
    if ([NSString mdf_isEmptyString:message]) {
        message = @"请求失败，请稍后重试！";
    }
    
    NSString *resultImgName;
    switch (maskType) {
        case SV_Result_Success://成功图样
            resultImgName = @"success";
            break;
        case SV_Result_Error://失败图样
            resultImgName = @"error";
            break;
        case SV_Result_Warning://警示图样
            resultImgName = @"info";
            break;
        case SV_Result_CryFace://哭脸图样
            resultImgName = @"face";
            break;
        default:
            resultImgName = @"____";
            break;
    }
    
    NSString *showMessage = message;

    [self setDefaultStyle:SVProgressHUDStyleDark];
    [self setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [self setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [self setMinimumDismissTimeInterval:minDalayTime];
    
    UIImage *resultImg = [UIImage imageWithContentsOfFile:[[self SV_IMGBundle] pathForResource:resultImgName ofType:@"png"]];
    [self showImage:resultImg status:showMessage];
    
    NSTimeInterval displayInterval = [self displayDurationForString:showMessage];
    
    [self doCompletionBlock:completedBlock Delay:displayInterval];
}

#pragma mark - 手动隐藏
+ (void)hideHUD {
    [self dismiss];
}

#pragma mark - doCompletionBlock
+ (void)doCompletionBlock:(void (^)(void))completion Delay:(NSTimeInterval)delayTime {
    if (completion) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((delayTime + 0.1) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            completion();
        });
    }
}

#pragma mark - loadBundle
+ (NSBundle *)SV_IMGBundle {
    NSBundle *bundle = [NSBundle bundleForClass:[SVProgressHUD class]];
    NSURL *url = [bundle URLForResource:@"SVProgressHUD" withExtension:@"bundle"];
    NSBundle *imageBundle = [NSBundle bundleWithURL:url];
    
    return imageBundle;
}

@end
